FROM smart-home-dev-harbor.miligc.com/public/node:16.20.2 as build-stage

# 定义一个名为BUILD_ENV的参数，可以在构建时使用--build-arg BUILD_ENV=value来指定
ARG BUILD_ENV=dev

# 设置环境变量ENVIRONMENT，其值取决于BUILD_ENV参数
ENV ENVIRONMENT=${BUILD_ENV}

WORKDIR /app

COPY package*.json ./

# 关闭 SSL 严格检查（注意安全风险）
RUN npm config set strict-ssl false

# 设置 npm 安装过程的超时和重试参数
RUN npm config set fetch-retry-mintimeout 20000
RUN npm config set fetch-retry-maxtimeout 120000

# 设置 npm 使用阿里云的 registry
RUN npm config set registry https://registry.npmmirror.com/

# 显示当前配置的 registry，主要是为了验证配置已经生效
RUN npm config get registry

RUN npm install

COPY . .

# 根据ENVIRONMENT环境变量的值来决定使用哪个构建命令
RUN if [ "$ENVIRONMENT" = "test" ]; then \
        npm run build:test; \
    elif [ "$ENVIRONMENT" = "prod" ]; then \
        npm run build:prod; \
    else \
        npm run build:dev; \
    fi

FROM smart-home-dev-harbor.miligc.com/public/nginx:1.23.0

COPY --from=build-stage /app/dist /data

RUN /bin/bash -c 'echo init ok'
